public class MissingTwo {
  public int[] missingTwo(int[] nums) {
      int n = nums.length + 2;
      
      // Calculate expected and actual sums
      long expectedSum = (long)(n + 1) * n / 2;
      long actualSum = 0;
      
      // Calculate expected and actual sum of squares
      long expectedSquareSum = (long)n * (n + 1) * (2 * n + 1) / 6;
      long actualSquareSum = 0;
      
      for (int num : nums) {
          actualSum += num;
          actualSquareSum += (long)num * num;
      }
      
      // Let x and y be the missing numbers
      long sumXy = expectedSum - actualSum; // x + y
      long squareSumXy = expectedSquareSum - actualSquareSum; // x² + y²
      
      // Using formulas: (x - y)² = x² + y² - 2xy
      // And xy = [(x + y)² - (x² + y²)] / 2
      long xy = (sumXy * sumXy - squareSumXy) / 2;
      
      // Now we have x + y = sumXy and xy = xy
      // We can solve for x and y using quadratic equation
      // Let's find x such that x is one of the missing numbers
      int[] result = new int[2];
      for (long x = 1; x <= n; x++) {
          long y = sumXy - x;
          if (x * y == xy && y >= 1 && y <= n) {
              result[0] = (int)x;
              result[1] = (int)y;
              break;
          }
      }
      
      return result;
  }

  public static void main(String[] args) {
      int[] nums = {2, 3};
      int[] result = new MissingTwo().missingTwo(nums);
      for (int i : result) {
          System.out.print(i + " ");
      }
  }
}


// 给定一个数组，包含从 1 到 N 所有的整数，但其中缺了两个数字。你能在 O(N) 时间内只用 O(1) 的空间找到它们吗？

// 以任意顺序返回这两个数字均可。

// 示例 1：

// 输入：[1]
// 输出：[2,3]
// 示例 2：

// 输入：[2,3]
// 输出：[1,4]
// 提示：

// nums.length <= 30000